#!/bin/bash

set -e

EA_PATH=$HOME/.emacs_anywhere
EA_URL='https://github.com/zachcurry/emacs-anywhere.git'

err () { echo -e "Error: $@" 1>&2; }

installed () { hash $1 &> /dev/null; }

require_deps () {
    for d in $@; do
        if installed $d ; then
            echo dependency check passed: $d
        else
            err dependency check failed: "'$d'" not found
            exit 1
        fi
    done
}

case $OSTYPE in
    "darwin"*)
        WRKFLW_PATH=$EA_PATH/Emacs\ Anywhere.workflow
        SERVICES_PATH=$HOME/Library/Services
        ;;
    "linux-gnu")
        require_deps xclip xdotool xprop xwininfo
        ;;
    *)
        err OS $OSTYPE isn\'t currently supported
        exit 1
        ;;
esac

require_deps emacsclient git

if [ -a $EA_PATH ]; then
    echo 'Uninstalling existing installation...'
    rm -rf $EA_PATH
    echo 'Reinstalling Emacs Anywhere...'
else
    echo "Installing Emacs Anywhere..."
fi

git clone $EA_URL $EA_PATH

if [[ $OSTYPE == "darwin"* ]]; then
    echo "Copying $WRKFLW_PATH to ${SERVICES_PATH}..."
    cp -Rf "${WRKFLW_PATH}" $SERVICES_PATH
fi

echo Emacs Anywhere has been installed!
